package world;

import math.Vector3f;

public class Vertex {
	// The amount of bytes an element has
	public static final int BYTE_SIZE_PER_ELEMENT = 4;

	// Elements per parameter
	public static final int SIZE_POSITION = 4;
	public static final int SIZE_TEXTURE = 2;
	public static final int SIZE_COLOR = 1;


	// Bytes per parameter
	public static final int BYTE_SIZE_POSITION = SIZE_POSITION * BYTE_SIZE_PER_ELEMENT;
	public static final int BYTE_SIZE_TEXTURE = SIZE_TEXTURE * BYTE_SIZE_PER_ELEMENT;
	public static final int BYTE_SIZE_COLOR = SIZE_COLOR * BYTE_SIZE_PER_ELEMENT;

	// Byte offsets per parameter
	public static final int OFFSET_POSITION = 0;
	public static final int OFFSET_TEXTURE = OFFSET_POSITION + BYTE_SIZE_POSITION;
	public static final int OFFSET_COLOR = OFFSET_TEXTURE + BYTE_SIZE_TEXTURE;

	// The amount of elements that a vertex has
	public static final int SIZE_ALL = SIZE_POSITION + SIZE_TEXTURE + SIZE_COLOR;
	public static final int STRIDE = BYTE_SIZE_POSITION + BYTE_SIZE_COLOR + BYTE_SIZE_TEXTURE;

	private float[] elements = new float[SIZE_ALL];
	
	public Vertex setXYZ(Vector3f v) {
		return this.setXYZW(v.x, v.y, v.z, 1f);		
	}
	
	public Vertex setXYZ(float x, float y, float z) {
		return this.setXYZW(x, y, z, 1f);		
	}

	public Vertex setXYZW(float x, float y, float z, float w) {
		this.elements[0] = x;
		this.elements[1] = y;
		this.elements[2] = z;
		this.elements[3] = w;
		return this;
	}

	public Vertex setST(float s, float t) {
		this.elements[4] = s;
		this.elements[5] = t;
		return this;
	}

	public Vertex setRGB(int r, int g, int b) {
		return this.setRGBA(r, g, b, 1);
	}

	public Vertex setRGBA(int r, int g, int b, int a) {
		if (r > 255) {
			r = 255;
		} else if (r < 0) {
			r = 0;
		}

		if (g > 255) {
			g = 255;
		} else if (g < 0) {
			g = 0;
		}

		if (b > 255) {
			b = 255;
		} else if (b < 0) {
			b = 0;
		}

		this.elements[6] = r << 24 | g << 16 | b << 8 | 0;
		return this;
	}


	public float[] getElements() {
		return elements;
	}
}